// pages/afterSales/afterSales.js
const app = getApp();
const api = require('../../utils/api');
const util = require('../../utils/util');
const constant = require('../../utils/constant');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURLWWW:constant.BASEURLWWW,
    // 订单编号
    order_no: '',
    // tabNav
    tabNav:[
      {id:0,title:"售后申请"},
      {id:1,title:"处理中"},
      {id:2,title:"申请记录"},
    ],
    // tabNav索引
    current: 0, 
    // 所有的订单
    afterSalesOrderListData: [],
    isLastNotice: false,
    // 是否有数据
    haveData: true,
    // 是否申请售后
    isApply: false,
    // 订单商品分页
    goodsPagination:{
      pageNum: 1,  
      pageSize: 10, 
      total: null
    },
    // 分页
    pagination:{
      pageNum: 1,
      pageSize: 10, 
      total: null
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.order_no){
      this.getOrderGoodsListData(options.order_no,this.data.goodsPagination);
    }else if(options.current){
      this.setData({
        current: options.current
      })
      this.getInProcessList(0,options.current,this.data.pagination);
    }else{
      this.getAfterSalesOrderListData(this.data.pagination);
    }
  },
  // 获取指定订单下的商品
  getOrderGoodsListData:function(order_no,goodsPagination){
    api.getAfterSalesGoodsList(order_no,goodsPagination).then(res => {
      if(res.code == 0){
        if(res.data.data.total != 0){
          res.data.data.list.forEach(item => {
            let i = 0;
            item.isApply = false;
            item.isAllChoose = false;
            item.order_goods.forEach(goods => {
              if(goods.is_service == 0){
                goods.isChoose = false;
                i++;
              }
            })
            // 是否可以申请售后
            if(i<=0){
              item.isCanApply = false;
            }else{
              item.isCanApply = true;
            }
          })
          this.setData({
            order_no,
            afterSalesOrderListData: res.data.data.list,
            haveData:true
          })
        }
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  // 获取所有订单
  getAfterSalesOrderListData:function(current,pagination){
    let afterSalesOrderListDataArr = [];
    if(this.data.current == current){
      afterSalesOrderListDataArr.push(...this.data.afterSalesOrderListData);
    }
    if(afterSalesOrderListDataArr.length == this.data.pagination.total){
      this.setData({
        isLastNotice: true,
      })
    }else{
      // 开始请求
      wx.showLoading();
      api.getAfterSalesOrderList(pagination).then(res => {
        if(res.code == 0){
          if(res.data.data.total != 0){
            res.data.data.list.forEach(item => {
              let i = 0;
              item.isApply = false;
              item.isAllChoose = false;
              item.order_goods.forEach(goods => {
                if(goods.is_service == 0){
                  goods.isChoose = false;
                  i++;
                }
              })
              // 是否可以申请售后
              if(i<=0){
                item.isCanApply = false;
              }else{
                item.isCanApply = true;
              }
            })
            afterSalesOrderListDataArr.push(...res.data.data.list);
            this.setData({
              order_no:'',
              afterSalesOrderListData: afterSalesOrderListDataArr,
              haveData:true,
              'pagination.total':  res.data.data.total,
              isLastNotice: false,
            })
          }
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          })
        }
        wx.hideLoading();
      })
    }
    
  },
  // 获取处理中
  getInProcessList:function(trade_status,current,pagination){
    let afterSalesOrderListDataArr = [];
    if(this.data.current == current){
      afterSalesOrderListDataArr.push(...this.data.afterSalesOrderListData);
    }
    if(afterSalesOrderListDataArr.length == this.data.pagination.total){
      this.setData({
        isLastNotice: true,
      })
    }else{
      // 开始请求
      wx.showLoading();
      api.getInProcessList(trade_status,pagination).then(res => {
        if(res.code == 0){
          if(res.data.data.total != 0){
            afterSalesOrderListDataArr.push(...res.data.data.list);
            this.setData({
              order_no:'',
              afterSalesOrderListData: afterSalesOrderListDataArr,
              haveData:true,
              'pagination.total':  res.data.data.total,
              isLastNotice: false,
            })
          }
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          })
        }
        wx.hideLoading();
      })
    }
  },
  // 上拉加载
  bindscrolltolower:function(){
    let pagination = {
      pageNum: this.data.pagination.pageNum + 1,
    }
    this.setData({
      'pagination.pageNum': pagination.pageNum
    })
    if(this.data.current == 0){
      this.getAfterSalesOrderListData(this.data.current,this.data.pagination);
    }
    if(this.data.current == 1){
      this.getInProcessList(0,this.data.current,this.data.pagination);
    }
    if(this.data.current == 2){
      this.getInProcessList('',this.data.current,this.data.pagination);
    }
  },
  // 删除订单搜索
  deleteSearch:function(){
    this.getAfterSalesOrderListData(this.data.pagination);
  },
  // 返回上一级
  back:function(){
    wx.reLaunch({
      url: '/pages/personal/personal'
    })
  },
  // 选项卡切换
  tabSwitch:function(e){
    this.setData({
      current: e.currentTarget.dataset.id
    })
  },
  // 选项卡滑动
  tabChange:function(e){
    this.setData({
      current: e.detail.current,
      'pagination.pageNum': 1,
      afterSalesOrderListData: []
    })
    // 售后申请
    if(e.detail.current == 0){
      this.getAfterSalesOrderListData(this.data.pagination);
    }
    // 售后处理中
    if(e.detail.current == 1){
      this.getInProcessList(0,this.data.pagination);
    }
    // 售后处理中
    if(e.detail.current == 2){
      this.getInProcessList('',this.data.pagination);
    }
  },
  // 获取文本框值
  bindinput:function(e){
    let inputValue = util.trimStr(e.detail.value);
    if(inputValue == ''){
      this.getAfterSalesOrderListData(this.data.pagination);
    }
    this.setData({
      order_no: inputValue
    })
  },
  // 搜索订单
  searchOrder:function(){
    if(this.data.order_no == ''){
      this.getAfterSalesOrderListData(this.data.pagination);
    }else{
      this.getOrderGoodsListData(this.data.order_no,this.data.goodsPagination);
    }
  },
  // 全选
  allChoose:function(e){
    let order_id =  e.currentTarget.dataset.orderid;
    let afterSalesOrderListData = this.data.afterSalesOrderListData;
    afterSalesOrderListData.forEach(item => {
      if(item.order_id == order_id){
        if(item.isAllChoose){
          item.isAllChoose = false;
          item.order_goods.forEach(goods => {
              goods.isChoose = false;
          })
        }else{
          item.isAllChoose = true;
          item.order_goods.forEach(goods => {
              goods.isChoose = true;
          })
        }
      }
    })
    this.setData({
      afterSalesOrderListData
    })
  },
  // 单选
  goodsChoose:function(e){
    let order_id = e.currentTarget.dataset.orderid;
    let goods_id = e.currentTarget.dataset.goodsid;
    let afterSalesOrderListData = this.data.afterSalesOrderListData;
    let i = 0;
    afterSalesOrderListData.forEach(item => {
      if(item.order_id == order_id){
        item.order_goods.forEach(goods => {
          if(goods.goods_id == goods_id){
            if(goods.isChoose == true){
              goods.isChoose = false;
            }else{
              goods.isChoose = true;
            }
          }
          if(goods.isChoose == true){
            i++;
          }
        })
        if(item.order_goods.length == i){
          item.isAllChoose = true;
        }else{
          item.isAllChoose = false;
        }
      }
    })
    this.setData({
      afterSalesOrderListData
    })
  },
  // 申请售后
  apply:function(e){
    let order_id = e.currentTarget.dataset.orderid;
    let afterSalesOrderListData = this.data.afterSalesOrderListData;
    let i = 0; 
    afterSalesOrderListData.forEach(item => {
      item.isApply = false;
      if(item.order_id == order_id){
        item.isApply = true;
      }
    })
    this.setData({
      afterSalesOrderListData
    })
  },
  // 取消售后
  applyCancel:function(e){
    let order_id = e.currentTarget.dataset.orderid;
    let afterSalesOrderListData = this.data.afterSalesOrderListData;
    afterSalesOrderListData.forEach(item => {
      if(item.order_id == order_id){
        item.isApply = false;
        item.isAllChoose = false;
        item.order_goods.forEach(goods => {
          if(goods.isChoose){
            goods.isChoose = false;
          }
        })
      }
    })
    this.setData({
      afterSalesOrderListData
    })
  },
  // 确认售后
  applyConfirm:function(e){
    let that = this;
    let order = e.currentTarget.dataset.order;
    console.log(order);
    // 申请售后对象
    let applyObj = {};
    applyObj.order_id = order.order_id;
    applyObj.order_goods = [];
    order.order_goods.forEach(item => {
      let goods = {};
      if(item.is_service == 0 && item.isChoose == true){
        goods.order_goods_id = item.order_goods_id;
        if(item.goods_pic[0]){
          item.goods_pic[0].url_x160 == '' ? (item.goods_pic[0].url_x800 == '' ? goods.goods_pic = that.data.BASEURLWWW+item.goods_pic[0].url : goods.goods_pic = that.data.BASEURLWWW+item.goods_pic[0].url_x800) : goods.goods_pic = that.data.BASEURLWWW+item.goods_pic[0].url_x160;
        }
        goods.goods_name = item.goods_name;
        goods.goods_num = item.qty;
        goods.goods_price = item.shop_price;
        applyObj.order_goods.push(goods);
      }
    })
    if(applyObj.order_goods.length <= 0){
      wx.showToast({
        title: '请选择需要申请售后的商品',
        icon: 'none'
      })
    }else{
      wx.navigateTo({
        url: `/pages/afterSalesApply/afterSalesApply?goods=${JSON.stringify(applyObj)}`,
      })
    }
    
  }
})